package ynu.edu.elmspringboot.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import ynu.edu.elmspringboot.po.User;
import ynu.edu.elmspringboot.service.UserService;
import ynu.edu.elmspringboot.utils.JwtUtil;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/UserController")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;
//    @PostMapping("/login")
//    public User Login(User user){
////        return userService.Login(user);
//        return userService.getOne(new QueryWrapper<User>().eq("user_id", user.getUserId()).eq("password", user.getPassword()));
//    }

    @PostMapping("/login")
    public Map<String, String> login(@RequestBody User user, HttpServletResponse response) {
        User existingUser = userService.getOne(new QueryWrapper<User>()
                .eq("user_id", user.getUserId())
                .eq("password", user.getPassword()));
        if (existingUser != null) {
            Map<String, Object> claims = new HashMap<>();
            claims.put("userId", existingUser.getUserId());

            String jwtToken = JwtUtil.generateJwt(claims);

            Map<String, String> tokenMap = new HashMap<>();
            tokenMap.put("token", jwtToken);

            log.info(" JWT Token: {}", jwtToken);
            return tokenMap;
        } else {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            return null;
        }
    }
    @PostMapping("/getUserById")
    public int getUserById(User user) {
        //通过userid查询数据库中count(user_id)
         return (int) userService.count(new QueryWrapper<User>().eq("user_id", user.getUserId()));
    }

    @PostMapping("/register")
    public int saveUser(User user) {
        log.info("用户注册: {}", user);
        return userService.save(user) ? 1 : 0;
    }

//    @PostMapping("/getUserInfo")
//    public User getUserInfo(String userId) {
//        // 使用 QueryWrapper 只查询用户信息
//        return userService.getOne(new QueryWrapper<User>()
//                .eq("user_id", userId));
//    }

    @GetMapping("/getUserInfo")
    public User getUserInfo( @RequestParam String userId) {
//        if (authorizationHeader == null || !authorizationHeader.startsWith("Bearer ")) {
//            return null; // 或者抛出异常
//        }
//        String jwt = authorizationHeader.replace("Bearer ", "");
//        Claims claims = JwtUtil.parseJWT(jwt);
//        String currentUserID = claims.get("userId", String.class);
//        if (currentUserID != null && currentUserID.equals(userId)) {
//            return userService.getOne(new QueryWrapper<User>().eq("user_id", userId));
//        } else {
//            return null;
//        }
        return userService.getOne(new QueryWrapper<User>().eq("user_id", userId));
    }
}
